﻿
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Data;
using Newtonsoft.Json.Linq;

namespace Test_API
{
    class WikiTest : AnnouncementTest
    {
        protected override string moduleName
        {
            get
            {
                return "Wiki Test";
            }
        }

        private string viewVersionMethod;
        private List<int> versionIDs;

        public WikiTest(string accessToken, string cid)
            : base(accessToken, cid)
        {
            this.viewAllMethod = "viewAllWikis";
            this.viewSingleItemMethod = "viewWiki";
            this.viewVersionMethod = "viewWikiVersion";
            this.deleteMethod = "deleteWiki";
            this.addMethod = "addWiki";
            this.updateMethod = "updateWiki";
            this.cid = cid;

            this.versionIDs = new List<int>();
        }

        protected override string genericQueryStringGenerate(int validItemID)
        {
            string queryString = new Dictionary<string, string>() { { "cid", this.cid }, { "accesstoken", this.accesstoken }, { "itemid", validItemID.ToString() }, { "versionid", this.versionIDs.randomElement().ToString() } }.buildQueryString();

            return queryString;
        }
        protected override Dictionary<string, object> genericPostDataSetGenerate()
        {
            Dictionary<string, object> dataSet = new Dictionary<string, object>();
            {
                dataSet.Add("body", Library.getDescription());
                dataSet.Add("title", Library.getWord()+"-"+Library.getWord());
            }
            return dataSet;
        }

        private List<LogEntry> viewSingleVersion()
        {
            List<LogEntry> entries = base.viewAll();
            if (entries[0].verdict == false)
            {
                return entries;
            }

            //List<LogEntry> entries = new List<LogEntry>();
            LogEntry t1 = new LogEntry(this.moduleName, true);
            LogEntry tempLog = entries[0];

            JObject receivedJson = JObject.Parse(tempLog.receivedData);
            JArray idsFromServer = (JArray)receivedJson["dataSet"][0]["versionIds"];
            foreach (var item in idsFromServer)
            {
                this.versionIDs.Add((int)item);
            }
            HTTP.execute(HTTP.RequestType.GET, this.rootUrl, this.viewVersionMethod, this.genericQueryStringGenerate(this.validItemIDs.randomElement()), null, ref t1);

            entries.Add(t1);
            return entries;
        }

        public new List<LogEntry> start()
        {
            Console.WriteLine("Wiki API call begin\n============");
            List<LogEntry> entries = new List<LogEntry>();

            entries.AddRange(this.addOrUpdate(true));
            entries.AddRange(this.viewAll());
            entries.AddRange(this.viewSingle());
            entries.AddRange(this.addOrUpdate(false));
            entries.AddRange(this.viewSingleVersion());            
            entries.AddRange(this.delete());
            return entries;
        }
    }
}